package org.zhaolei.util;

import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import org.zhaolei.entity.auth.AuthUserEntity;

@Component
public class PasswordUtil {

    @Value("${mall.mgt.password.privateKey:MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCTv+k8PlmQ5qtM\n" +
            "qN8mSMkJAjsePvS4TCYpUJtnewoqP3RbJWRC0+JlNO+hLg2pMSnjL9FRKDqBMCda\n" +
            "5V2gVrqWEu6EdWjVSXRCBhSETUwqwQBIqyKOJZNN3ZmmOm3SAfwm5segP/oJsP4m\n" +
            "nf7RDMqPifXrr4Lcg4+jvDOB4j33PYiLbw6UvpI/JJorc3CzZSptAAG6makcgHcP\n" +
            "8FahwPh6uVL0AuAagh78F3Z9HRYrtjxlChBQ2UGA6rOPofCNj6e2JJSv3/20Q5xY\n" +
            "tIEH2VfGYL7fR4w1i5h56NuELQOfyuq5rTDphfCk75ZgC3vBHkrVwq3csMrBhcWm\n" +
            "B9ZYoqfTAgMBAAECggEALJakCpw+Z25Vj+L+xQz+LovPz4bsT5obgihjXc1R+gIA\n" +
            "WWllYo2TKzvYfWnqkX7Wx0ss/jMHxOkFV3cU1EiYMqYv+SV9p7BtN2eiVXMGY3iv\n" +
            "Xz7WRNOuKrePoQzNC62xrQPEhuYHzVjuujTAK9KSZtMZtjDXazkA5kbrvGzMiopA\n" +
            "ADqjUdeO3ViOILQoakpwuEzNwKrVL2U4STs1K0S1mLHQSpBsSeTJ7kuHLyB1TjqF\n" +
            "I86xFqHjMPYUHKx3GyhkWdJ9WJw23EUbZKdRwuvuJPQFGqDYo0WOVwBBkASKgVDh\n" +
            "Jb72vowZ+WkoLFYC8amqKKzCKXsmisPUnRrSbXx50QKBgQDEraTqiNJF1qh/PZP7\n" +
            "IxOKyYuPvw3YzcEdasE1OtSy1W96ddjvDeXISqY1l/8s885jvbFn9pwzudA8wVbp\n" +
            "345H8v7KF3+Z7KDAE/a2zbkgruHj5vMwqwku6Vp4TmGz4kiqVSuAnsZ6KFdOz+gn\n" +
            "9uRXMLDTZtyN5dtEVg4/m5BLHwKBgQDAUEf1i/NUp43LBkftFG/SL9ke1tsvrHRV\n" +
            "L9FB/PETbpH7IA3XwNMRGAp8yjLZKMopZGC/r4zxyFRcNbSfmeW7lPrHAi8OKgIu\n" +
            "l+h/qxorblhwEqG37nU9oGrc4Iu5svRoo6uYCrewnDBLokhbBEiodqrwuGTynT8v\n" +
            "KORx5ZmAzQKBgQC58xq7C8USdpM0SfoBNPai/07OB2qGEnKE54voUckLk7QkagtC\n" +
            "aTdZGQrwHNcLny8BKjXy/zxLw2BYMMuFrvGMoPe4BmOEGi8Zq0ZVK0fe2LmFibNf\n" +
            "zjWOft4kZKcHNehR8tiAcJ1inYV+ZB8wucLFlKl6FgRsvGeEwoASL3hLLQKBgQCR\n" +
            "lTSkQYm3MBA6/Uo9wxbTer1WhzfuYxK92jEDT0QAfdesz5srFSGy8wwdY8QSeLpx\n" +
            "kzdaZL7B2fFizJUrrQkoc6NUO+b7RfnY485xt0lcT3BLhw89ehruyB2dj662qS40\n" +
            "eJJRyus7m0FEQFmd7SRDw3GsQSrKLxfBz6cAQdtukQKBgQCVsjfgbo4in+H3LVpN\n" +
            "0kuoYQRcodVKpgdLXlkSlevS/YADHvSoih0adD857baa9GM8CaGp0AJbn1LEbmvH\n" +
            "7Cke03wt3dd3iHBgN8EJSXLWIF1ATzQjf5VnOBredF/E2nP24lFrc+NaMnMxJAbG\n" +
            "R+e39zBpiEvMC7zzPKLWct6jpw==}")
    private String privateKey;

    @Autowired
    private PasswordEncoder passwordEncoder;

    private PasswordUtil() {
    }


    public String encode(String password) {
        return passwordEncoder.encode(password);
    }

    /**
     * 解密RSA密码
     *
     * @param authUserEntity 用户实体
     * @return 密码
     */
    public String decodeRsaPassword(AuthUserEntity authUserEntity) {
        RSA rsa = new RSA(privateKey, null);
        return new String(rsa.decrypt(authUserEntity.getPassword(), KeyType.PrivateKey));
    }
}
